<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class ShopService extends Service {
	
	public static function oneHeader($params){
        $header = array(
            'RecDate'       => array('text' => '日期'),
            'SID'           => array('text' => '服'),
        	'CurrencyType'  => array('text' => '货币类型'),
            'GoodsId'       => array('text' => '物品ID'),
            'GoodsName'     => array('text' => '物品名称'),
        	'GoodsPrice'    => array('text' => '物品单价'),
            'PayUser'       => array('text' => '购买玩家数','rule'=>'numeric'),
            'PayCount'      => array('text' => '购买次数','rule'=>'numeric'),
            'PayMoney'      => array('text' => '购买货币总数','rule'=>'numeric'),
        	'PerPayMoney'   => array('text' => '货币占比','rule'=>'percent'),
        	'AvgPayMoney'   => array('text' => '平均购买货币数','rule'=>'numeric'),
        );
        if($params['server'] == 'ALL'){
        	unset($header['SID']);
        }

        return $header;
    }
    
    public static function oneData($params){
    	$data = ModShop::oneData($params);
    
    	if($params['time_start'] == $params['time_end']){
    		$date = $params['time_start'];
    	}else{
    		$date = date('Y.m.d', strtotime($params['time_start'])).'~'.date('Y.m.d', strtotime($params['time_end']));
    	}
    	
    	$data = self::goodsData($data);
    	
    	$coinTypeList = PubDict::dict('mango', 'currencytype');
    	
        $new_data = array();
        foreach ($data as $key=>$val){
        	$new_data[$val['CurrencyType']] += $val['PayMoney'];
        }
    
    	foreach($data as &$v){
    		$v['RecDate'] = $date;
    		$v['SID'] = $params['server'];
    		$v['PerPayMoney'] = number_format($v['PayMoney'] / $new_data[$v['CurrencyType']] * 100, 2).'%';
    		$v['CurrencyType'] = $coinTypeList[$v['CurrencyType']];
    		$v['AvgPayMoney'] = number_format($v['PayMoney'] / $v['PayUser'], 2);
    	}
    	unset($v);
    
    	return $data;
    }
    
    public static function allHeader($params){
    	$header = array(
    			'RecDate'       => array('text' => '日期'),
    			'SID'           => array('text' => '服'),
    			'CurrencyType'  => array('text' => '货币类型'),
    			'PayUser'       => array('text' => '总购买玩家数','rule'=>'numeric'),
    			'PayCount'      => array('text' => '总购买次数','rule'=>'numeric'),
    			'PayMoney'      => array('text' => '总消耗货币数','rule'=>'numeric'),
    			'AvgPayCount'   => array('text' => '平均每人购买次数','rule'=>'numeric'),
    			'AvgPayMoney'   => array('text' => '平均每人购买消耗货币','rule'=>'numeric'),
    	);
    	if($params['server'] == 'ALL'){
    		unset($header['SID']);
    	}
    
    	return $header;
    }
    
    public static function allData($params){
    	$data = ModShop::allData($params);
    
    	if($params['time_start'] == $params['time_end']){
    		$date = $params['time_start'];
    	}else{
    		$date = date('Y.m.d', strtotime($params['time_start'])).'~'.date('Y.m.d', strtotime($params['time_end']));
    	}
    	 
    	$coinTypeList = PubDict::dict('mango', 'currencytype');
    
    	foreach($data as &$v){
    		$v['RecDate'] = $date;
    		$v['SID'] = $params['server'];
    		$v['CurrencyType'] = $coinTypeList[$v['CurrencyType']];
    		$v['AvgPayCount'] = number_format($v['PayCount'] / $v['PayUser'], 2);
    		$v['AvgPayMoney'] = number_format($v['PayMoney'] / $v['PayUser'], 2);
    	}
    	unset($v);
    
    	return $data;
    }
    
    
    /**
     * 处理数据获取物品名称
     * @param unknown $data
     */
    public static function goodsData($data){
    	$g_id_array = array();
    	foreach ($data as $key=>$val){
    		if($val['GoodsId'] == ''){
    			continue;
    		}
    		$g_id_array[] = $val['GoodsId'];
    	}
    	$modGoods = new ModGood();
    	$goods_data= $modGoods->getInfo($g_id_array);
    	$new_goods_data = array();
    	foreach ($goods_data as $key=>$val){
    		$new_goods_data[$val['g_id']] = $val;
    	}
    	foreach($data as &$v){
    		$v['GoodsName'] = $new_goods_data[$v['GoodsId']]['name'] ;
    	}
    	unset($v);
    	 
    	return $data;
    }

    public static function indexHeader(){
        $header = array(
            'RecDate'   => array('text' => '日期'),
            'CoinType'  => array('text' => '货币类型'),
            'SID'       => array('text' => '服'),
            'ItemId'    => array('text' => '物品ID'),
            'ItemName'  => array('text' => '物品名称'),
            'UsedCoin'  => array('text' => '花费货币','rule'=>'numeric'),
            'GetNumber' => array('text' => '获得人数','rule'=>'numeric'),
            'GetCnt'    => array('text' => '获得人次','rule'=>'numeric'),
        );

        return $header;
    }

    public static function indexData($params,$dict = array()){
        $data = ModShop::indexData($params);

        PubFunc::parseDict($data,$dict);

        $total = array();
        foreach($data as $k=>$v){
            if($v['ItemId'] == 'ALL'){
                $total = $v;
                unset($data[$k]);
            }
        }

        $pubSort = new PubMultiSort();
        $pubSort->setSort('UsedCoin','desc');
        $data = $pubSort->getResult($data);

        return array(
            'data'  => $data,
            'total' => $total
        );
    }

    public static function blackHeader(){
        $header = array(
            'RecDate'   => array('text' => '日期'),
            'CoinType'  => array('text' => '货币类型'),
            'SID'       => array('text' => '服'),
            'ItemId'    => array('text' => '物品ID'),
            'ItemName'  => array('text' => '物品名称'),
            'GetNumber' => array('text' => '获得物品数','rule'=>'numeric'),
            'UsedCoin'  => array('text' => '花费货币','rule'=>'numeric'),
            'GetCnt'    => array('text' => '获得人次','rule'=>'numeric'),
            'Operation' => array('text' => '刷新次数','rule'=>'numeric'),
        );

        return $header;
    }

    public static function blackData($params,$dict = array()){
        $data = ModShop::blackData($params);

        PubFunc::parseDict($data,$dict);

        $total = array();
        foreach($data as $k=>$v){
            if($v['ItemId'] == 'ALL'){
                $total = $v;
                unset($data[$k]);
            }
        }

        $pubSort = new PubMultiSort();
        $pubSort->setSort('UsedCoin','desc');
        $data = $pubSort->getResult($data);

        return array(
            'data'  => $data,
            'total' => $total
        );
    }

    public static function indexChart($data, $params){
        $chartData = array();

        $i = 1;
        foreach($data as $v){

            if(empty($v['ItemName'])){
                $key = $v['ItemId'];
            }else{
                $key = $v['ItemId'] . '.' . $v['ItemName'];
            }
            $chartData['购买数量'][$key] = intval($v['GetNumber']);
            $chartData['花费货币'][$key] = intval($v['UsedCoin']);
            $i++;

            if($i > 20){
                break;
            }
        }

        return array(
            'data'   => $chartData,
            'config' => array(
                'series' => array(
                    '购买数量' => array(
                        'markLine' => array(
                        )
                    )
                )
            )
        );
    }


    public static function analysisInfo($params){
        $tmp    = ModShop::analysisData($params);

        $items  = array();
        foreach($tmp as $v){
            if($v['ItemId'] != 'ALL' && !isset($items[$v['ItemId']])){
                $items[$v['ItemId']] = array('text' => $v['ItemId'], 'rule'=>'numeric');
            }
        }

        $itemArr    = array_keys($items);
        //$dictItem   = PubDict::dictItem($itemArr);

        foreach($items as $k=>&$v){
            $v['text'] = $dictItem[$k] . ' - ' . $k;
        }
        unset($v);

        $header = array(
            'RecDate'   => array('text' => '日期'),
            'Mall'      => array(
                'text'      => '商城购买消耗(勾玉）',
                'include'   => $items
            ),
            'ALL'       => array('text' => '合计', 'rule'=>'numeric')
        );


        $data   = array();
        foreach($tmp as $v){
            $col = $v['ItemId'];

            if(!isset($data[$v['RecDate']])){
                $data[$v['RecDate']] = array(
                    'RecDate' => $v['RecDate']
                );
            }

            $data[$v['RecDate']][$col] = $v['UsedCoin'];
        }

        $total = array('RecDate' => '合计');
        foreach($data as &$v){
            foreach($items as $ik=>$iv){
                if(!isset($v[$ik])){
                    $v[$ik] = '--';
                }else{
                    $total[$ik] += $v[$ik];
                }
            }
            $total['ALL'] += $v['ALL'];
        }
        unset($v);

        $data['AVG'] = array('RecDate' => '占比');
        foreach($total as $k=>$v){
            if(!in_array($k, array('RecDate', 'ALL'))){
                $data['AVG'][$k] = number_format($v / $total['ALL'] * 100, 2) . '%';
            }
        }

        return array(
            'header' => $header,
            'data'   => array(
                'data'  => $data,
                'total' => $total
            )
        );
    }
} 
